package com.hb.znpt.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.hb.znpt.bean.UpdataBean;
import com.hb.znpt.model.OfflineModel;
import com.hb.znpt.utils.LogUtils;
import com.hb.znpt.utils.OssManager;
import com.rxjava.rxlife.RxLife;
import com.unitlib.base.base.BaseViewModel;
import com.unitlib.base.utils.ToastUtils;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * @Description: 描述
 * @Author: houbing
 * @CreateDate: 2022/1/4 16:42
 */
public class OfflineViewModel extends BaseViewModel<OfflineModel> {

    public MutableLiveData<List<UpdataBean>> listData = new MutableLiveData<>();

    public OfflineViewModel(@NonNull Application application) {
        super(application);
        model = new OfflineModel();
    }

    @Override
    public void onCreate() {
        getData();
    }

    public void getData() {
        listData.setValue(model.getData());
    }

    public void delete(UpdataBean bean) {
        bean.delete();
        getData();
    }

    public void updataImg(UpdataBean bean) {
        getUC().getShowLoadingDialogEvent().call();
        OssManager.getInstance().upload(bean.getPath(), new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onSuccess(PutObjectRequest request, PutObjectResult result) {
                LogUtils.e(TAG, "上传成功" +
                        "\nobjectKey:" + request.getObjectKey() +
                        "\nETag:" + result.getETag() +
                        "\nRequestId:" + result.getRequestId());
                upload(bean, request.getObjectKey());
            }

            @Override
            public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ToastUtils.showShort("图片上传失败，请检查手机网络是否畅通！");
                        getUC().getDismissLoadingDialogEvent().call();
                    }
                });
            }
        });
    }

    public void upload(UpdataBean bean, String url) {
        model.upload(Double.parseDouble(bean.getLat()), Double.parseDouble(bean.getLon()), url, bean.getZwid() + "")
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    getUC().getDismissLoadingDialogEvent().call();
                })
                .as(RxLife.as(owner))
                .subscribe(str -> {
                    bean.setFlag(1);
                    bean.update(bean.getId());
                    ToastUtils.showShort("上传成功！");
                    getData();
                }, error -> {
                    ToastUtils.showShort("上传失败，请检查手机网络是否畅通！");
                });
    }
}
